package com.sangmo.fts.process.storage.calculator;

import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.data.json.context.KeyValue;
import com.sangmo.boot.framework.data.json.load.Calculator;
import com.sangmo.fts.process.storage.model.CsfLoan;
import com.sangmo.fts.process.storage.model.CsfRepayment;
import com.sangmo.fts.process.storage.repo.CsfRepaymentRepo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Component
public class CsfRepaymentAmountCalculator implements Calculator<CsfLoan>, CsfLoan.ICsfLoanRepaymentCalculator {
    @Override
    public Object calc(CsfLoan bean, Collection<KeyValue> exts) {
        CsfRepaymentRepo csfRepaymentRepo = ApplicationUtils.getBean(CsfRepaymentRepo.class);
        List<CsfRepayment> repayments = csfRepaymentRepo.findByLoanId(bean.getId());
        BigDecimal repaymentAmount = BigDecimal.ZERO;
        for (CsfRepayment repayment : repayments) {
            repaymentAmount = repaymentAmount.add(repayment.getAmount());
        }
        return repaymentAmount;
    }
}
